package yuchen.ballanimation;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener ,Animator.AnimatorListener{

    private Ball ball;
    private ValueAnimator animator;

    private FrameLayout root;

    private boolean isMoved;

    private int endX;
    private int endY;

    private int lastX;
    private int lastY;

    private int lastLeft;
    private int lastTop;
    private int lastRight;
    private int lastButton;

    private int ballX;

    private Boolean isInHandler;
    private Boolean isStop;

    private final double speed = 2500;

    Handler myHandler = new Handler() {

        private ValueAnimator handleranimator;
        private Boolean isStop;

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 2:
                    isInHandler = true;
                    isStop = false;
                    Boolean isCal = false;
                    float rat = (float)Math.random();
                    if(rat > 0.8)
                    {
                        rat = (float) 0.8;
                    }
                    if(rat < 0.2)
                    {
                        rat = (float) 0.2;
                    }
                    Log.d("rat:",""+rat);
                    //Log.d("=.=.=.=.=.=.=",ball.getLeft()+ ","+ ball.getTop()+ "," + ball.getRight()+","+ball.getBottom());
                    if(((int)ball.getY() <=1 && (int)ball.getY() >=-1) && (int)ball.getX() != 0 && (int)(ball.getX()+ball.getWidth()) != root.getWidth() &&!isCal)
                    {
                        isCal = true;
                        float endX_s = Math.abs(endX);
                        float endY_s = Math.abs(endY);
                        if(endX < 0)
                        {
                            endX = 0 - (int)ball.getX();
                            float ratio = endY_s / endX_s;
                            float endY_f = ball.getX() * ratio;
                            endY = (int)endY_f;
                            if((ball.getY() + endY) > (root.getHeight() + ball.getWidth()))
                            {
                                endY = root.getHeight() - ball.getWidth();
                                float ratio2 = (endY_f - ball.getHeight())/endY_f;
                                endX = 0 - (int)(ball.getX() * (1.0-ratio2*0.6));
                                if(endX == 0)
                                {
                                    endX = 0 - (int)ball.getX()/2 + 20;
                                }
                                Log.d("+.+.+.+.+.+.==="," "+ endX+","+ratio2);
                            }
                        }else{
                            endX = root.getWidth() - (int)(ball.getX() + ball.getWidth());
                            float ratio = endY_s/endX_s;
                            float endY_f = (float)endX * ratio;
                            endY = (int)endY_f;
                            if((ball.getY() + endY) > (root.getHeight() + ball.getWidth()))
                            {
                                endY = root.getHeight() - ball.getWidth();
                                float ratio2 = (endY_f - ball.getHeight())/endY_f;
                                endX = (int)((float)(root.getWidth()-(int)(ball.getX() + ball.getWidth()))*(1.0-ratio2*0.6));
                            }
                        }
                    }
                    if((int)ball.getX() == 0 && (int)(ball.getY() + ball.getWidth()) != root.getHeight() && (int)ball.getY() != 0 && !isCal)
                    {

                        isCal = true;
                        float endY_s = Math.abs(endY);
                        float endX_s = Math.abs(endX);
                        if(endY > 0)
                        {
                            endY = root.getHeight() - (int)(ball.getY() + ball.getWidth());
                            float ratio = endX_s /endY_s;
                            float endX_f = endY * ratio;
                            endX = (int)endX_f;
                            if((ball.getX() + endX) > (root.getWidth() - ball.getWidth()))
                            {
                                endX = root.getWidth() - ball.getWidth();
                                float ratio2 = (endX_f - root.getWidth())/endX_f;
                                endY = (int)((root.getHeight() - (ball.getY() + ball.getWidth())) * (1.0-ratio2*0.8));
                            }
                        }else{
                            endY = 0 - (int)ball.getY();
                            float ratio = endX_s / endY_s;
                            float endX_f = (int)ball.getY() * ratio;
                            endX = (int)endX_f;
                            if((ball.getX() + endX) > (root.getWidth() - ball.getWidth()))
                            {
                                endX = root.getWidth() - ball.getWidth();
                                float ratio2 = (endX_f - root.getWidth())/endX_f;
                                endY = 0 - (int)(ball.getY() * (1.0-ratio2*0.8));
                            }
                        }
                    }
                    if((int)(ball.getY() + ball.getWidth()) == root.getHeight() && (int)ball.getX() != 0
                            &&  (int)(ball.getX() + ball.getWidth()) != root.getWidth() && !isCal)
                    {
                        isCal = true;
                        float endX_s = Math.abs(endX);
                        float endY_s = Math.abs(endY);
                        if(endX > 0)
                        {
                            endX = root.getWidth() - (int)(ball.getX() + ball.getWidth());
                            float ratio = endY_s / endX_s;
                            float endY_f = endX * ratio;
                            endY = 0 - (int)endY_f;
                            if((ball.getY() + endY) < 0)
                            {
                                endY = 0 - (int)(ball.getY());
                                float ratio2 = (endY_f - root.getHeight())/endY_f;
                                endX = (int)((root.getWidth() - (ball.getX() + ball.getWidth())) * (1.0-ratio2*0.8));
                            }

                        }else{
                            endX = 0 - (int)ball.getX();
                            float ratio = endY_s/endX_s;
                            float endY_f = ball.getX() * ratio;
                            endY = 0 - (int)endY_f;
                            if((ball.getY() + endY) < 0)
                            {
                                endY = 0 - (int)(ball.getY());
                                float ratio2 = (endY_f - root.getHeight())/endY_f;
                                endX = 0 - (int)(ball.getX() * (1.0-ratio2*0.8));
                            }
                        }
                    }
                    if((int)(ball.getX() + ball.getWidth()) == root.getWidth() && (int)ball.getY() != 0 && (int)(ball.getY() + ball.getWidth()) != root.getHeight() &&!isCal)
                    {
                        isCal = true;
                        float endX_s = Math.abs(endX);
                        float endY_s = Math.abs(endY);
                        if(endY < 0)
                        {
                            endY = 0 - (int)ball.getY();
                            float ratio = endX_s / endY_s;
                            float endX_f = ball.getY() * ratio;
                            endX = 0 - (int)endX_f;
                            if((ball.getX() + endX) < 0)
                            {
                                endX = 0 - (root.getWidth() - ball.getWidth());
                                float ratio2 = (endX_f - root.getWidth())/endX_f;
                                endY = 0 - (int)((float)ball.getY() * (1.0-ratio2*0.8));
                            }
                        }else{
                            endY = root.getHeight() - (int)(ball.getY() + ball.getWidth());
                            float ratio = endX_s/endY_s;
                            float endX_f = endY*ratio;
                            endX = 0 - (int)endX_f;
                            if((ball.getX() + endX) < 0)
                            {
                                endX = 0 - (root.getWidth() - ball.getWidth());
                                float ratio2 = (endX_f - root.getWidth())/endX_f;
                                endY = (int)((float)(root.getHeight() - (ball.getY() + ball.getWidth())) * (1.0-ratio2*0.8));
                            }
                        }
                    }
                    if(ball.getX() == ball.getY())
                    {
                        if(ball.getX() == 0) {
                            endY = root.getHeight() - root.getWidth();
                            endX = (int) (root.getWidth() * rat);
                        }else{
                            endY = 0 - (root.getHeight() - root.getWidth());
                            endX = 0 - (int) (root.getWidth() * rat);
                        }
                    }
                    if(ball.getX() == 0 && ball.getY() == (root.getHeight() - root.getWidth()))
                    {
                        endY = 0 - (root.getHeight() - root.getWidth());
                        endX = (int)(root.getWidth() * rat);
                    }
                    if(ball.getY() == 0 && ball.getX() == (root.getWidth() - root.getWidth()))
                    {
                        endY = root.getHeight() - root.getWidth();
                        endX = 0 - (int)(root.getWidth() * rat);
                    }

//                    if(ball.getLeft() == 0 && ball.getBottom() == root.getHeight())
//                    {
//                        endX = root.getWidth() -BALL_RADIUS;
//                        endY = -200;
//                    }
                    //Log.d("=======",ball.getX()+","+ball.getY());
                    //Log.d("=.=.=.=.=.=.=",ball.getX()+ ","+ ball.getTop()+ "," + (int)(ball.getY()+ball.getWidth())+","+(int)(ball.getY()+ball.getWidth()));
                    PointF start = new PointF(ball.getX(),ball.getY());
                    PointF end = new PointF(ball.getX()+endX,ball.getY()+endY);
                    //if(end.x > root.getWidth())
                    //animator = null;
                    if(end.x < 0)
                    {
                        if(end.y == 0 || end.y == (root.getHeight() - ball.getWidth()))
                        {
                            end.x = root.getWidth() * rat;
                        }else{
                            end.x = 0;
                        }
                        Log.e("__......","fixed");
                        end.x = 0;
                        endX = (int)(end.x - start.x);
                        endY = (int)(end.y - start.y);
                        //end.y = endY + (0 - end.x);
                    }
                    if(end.x > (root.getWidth() - ball.getWidth()))
                    {
                        if(end.y == 0 || end.y == (root.getHeight() - ball.getWidth()))
                        {
                            end.x = root.getWidth() * rat;
                        }else{
                            end.x = root.getWidth() - ball.getWidth();
                        }
                        Log.e("__......","fixed");

                        endX = (int)(end.x - start.x);
                        endY = (int)(end.y - start.y);
                    }
                    if(end.y < 0)
                    {
                        if(end.x == 0 || end.x ==(root.getWidth() - ball.getWidth()) )
                        {
                            end.y = root.getHeight() * rat;
                        }else{
                            end.y =  0;
                        }
                        Log.e("__......","fixed");
                        endY = (int)(end.y - start.y);
                        endX = (int)(end.x - start.x);
                    }
                    if(end.y > (root.getHeight() - ball.getWidth()))
                    {
                        if(end.x == 0 || end.x ==(root.getWidth() - ball.getWidth()) )
                        {
                            end.y = root.getHeight() * rat;
                        }else{
                            end.y = root.getHeight() - ball.getWidth();
                        }
                        Log.e("__......","fixed");

                        endY = (int)(end.y - start.y);
                        endX = (int)(end.x - start.x);
                    }
                    if(start.x == end.x)
                    {

                        end.x = root.getWidth() * rat;
                        endX = (int)(end.x - start.x);
                        endY = (int)(end.y - start.y);
                    }
                    else if(start.y == end.y)
                    {
                        end.y = root.getHeight() * rat;
                        endX = (int)(end.x - start.x);
                        endY = (int)(end.y - start.y);
                    }
                    handleranimator = ValueAnimator.ofObject(new PointEvaluator(),start,end);
                    //animator.setTarget(ball);
                    double diatane = MainActivity.getDistance(start,end);
                    double time = diatane / speed;
                    handleranimator.setDuration((int)(time * 1000)).start();
                    handleranimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            PointF currect = (PointF) animation.getAnimatedValue();
                            ball.setX(currect.x);
                            ball.setY(currect.y);
                        }
                    });
                    handleranimator.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            if(!isStop) {
                                Message msg = new Message();
                                msg.what = 2;
                                msg.obj = this;
                                myHandler.sendMessage(msg);
                            }else{
                                animation.cancel();
                                isStop = false;
                                Toast.makeText(getApplication(),"小球3秒后归位",Toast.LENGTH_SHORT).show();
                                new Handler().postDelayed(new Runnable(){
                                    public void run() {
                                        ball.setX(root.getWidth()/2 - ball.getWidth()/2);
                                        ball.setY(root.getHeight()/2 - ball.getWidth()/2);
                                    }
                                }, 3000);
                            }

                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {
                            isMoved = false;
                            isInHandler = false;
                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                    break;
                case 3:
                    this.isStop = true;
                    handleranimator.cancel();
                    ball.clearAnimation();
                    //isMoved = false;

                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ball = (Ball)findViewById(R.id.ball);
        ball.setOnClickListener(this);
        root = (FrameLayout)findViewById(R.id.root);
        ball.setOnTouchListener(this);
        isMoved = false;
        isInHandler = false;
        isStop = false;
    }


    @Override
    public void onClick(View v) {
        isMoved = true;
        if(isInHandler && isMoved) {
            Message msg = new Message();
            msg.what = 3;
            myHandler.sendMessage(msg);
        }else{
            isStop = true;
            animator.cancel();
        }
        Log.d("======","Click");

    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        //Log.d("+++++","Touch");
        if(!isMoved) {
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    lastLeft = (int)v.getX();
                    lastRight = lastLeft + v.getWidth();
                    lastTop = (int)v.getY();
                    lastButton = lastTop+v.getWidth();
                    Log.d("ACTION_DOWN+++++", lastX + "," + lastY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.d("++++++++", "MOVE");
                    int dx = (int) event.getRawX() - lastX;
                    int dy = (int) event.getRawY() - lastY;

                    int left = v.getLeft() + dx;
                    int top = v.getTop() + dy;
                    int right = v.getRight() + dx;
                    int bottom = v.getBottom() + dy;
                    if (left < 0) {
                        left = 0;
                        right = left + v.getWidth();
                    }
                    if (right > root.getWidth()) {
                        right = root.getWidth();
                        left = right - v.getWidth();
                    }
                    if (top < 0) {
                        top = 0;
                        bottom = top + v.getHeight();
                    }
                    if (bottom > root.getHeight()) {
                        bottom = root.getHeight();
                        top = bottom - v.getHeight();
                    }
                    v.layout(left, top, right, bottom);
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
                    isMoved = true;
                    int nowLeft = (int)v.getX();
                    int nowRight = nowLeft + v.getWidth();
                    int nowTop = (int)v.getY();
                    int nowButton = nowTop + v.getWidth();
                    Log.d("++++++++", "ActionUP");
                    if (Math.abs(nowLeft - lastLeft) < 5 && Math.abs(nowTop - lastTop) < 5) {
                        Log.d("==========", "Clicked");
                        Log.d("========", root.getWidth() + "," + root.getHeight());
                        isMoved = true;
                        endX = (int) ((ball.getWidth()/2 + 10) + Math.random() * (root.getWidth()/2 - (ball.getWidth()/2 + 10) + 1));
                        endY = (int) ((ball.getWidth()/2 + 10) + Math.random() * (root.getHeight()/2 - (ball.getWidth()/2 + 10) + 1));
                        int flag = (int) (1 + Math.random() * (4 - 1 + 1));
                        //flag = 4;
                        switch (flag) {
                            case 1:
                                endX = 0 - endX;
                                endY = 0 - (int)ball.getY();
                                break;
                            case 2:
                                endX = (int)ball.getX();
                                break;
                            case 3:
                                endY = 0 - (root.getHeight() - (int)ball.getY() - ball.getWidth());
                                endX = 0 - endX;
                                break;
                            case 4:
                                endY = (root.getHeight() - (int)ball.getY() - ball.getWidth());
                                break;
                        }
                        float rat = (float)Math.random();
                        PointF start = new PointF(ball.getX(), ball.getY());
                        PointF end = new PointF(ball.getX() + endX, ball.getY() + endY);
                        if(end.x < 0)
                        {
                            if(end.y == 0 || end.y == (root.getHeight() - ball.getWidth()))
                            {
                                end.x = root.getWidth() * rat;
                            }else{
                                end.x = 0;
                            }
                            Log.e("__......","fixed");
                            end.x = 0;
                            endX = (int)(end.x - start.x);
                            endY = (int)(end.y - start.y);
                            //end.y = endY + (0 - end.x);
                        }
                        if(end.x > (root.getWidth() - ball.getWidth()))
                        {
                            if(end.y == 0 || end.y == (root.getHeight() - ball.getWidth()))
                            {
                                end.x = root.getWidth() * rat;
                            }else{
                                end.x = root.getWidth() - ball.getWidth();
                            }
                            Log.e("__......","fixed");

                            endX = (int)(end.x - start.x);
                            endY = (int)(end.y - start.y);
                        }
                        if(end.y < 0)
                        {
                            if(end.x == 0 || end.x ==(root.getWidth() - ball.getWidth()) )
                            {
                                end.y = root.getHeight() * rat;
                            }else{
                                end.y =  0;
                            }
                            Log.e("__......","fixed");
                            endY = (int)(end.y - start.y);
                            endX = (int)(end.x - start.x);
                        }
                        if(end.y > (root.getHeight() - ball.getWidth()))
                        {
                            if(end.x == 0 || end.x ==(root.getWidth() - ball.getWidth()) )
                            {
                                end.y = root.getHeight() * rat;
                            }else{
                                end.y = root.getHeight() - ball.getWidth();
                            }
                            Log.e("__......","fixed");

                            endY = (int)(end.y - start.y);
                            endX = (int)(end.x - start.x);
                        }
                        animator = ValueAnimator.ofObject(new PointEvaluator(),start,end);
                        animator.setTarget(ball);
                        double diatane = MainActivity.getDistance(start,end);
                        double time = diatane / speed;
                        animator.setDuration((int)(time*1000)).start();
                        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                PointF currect = (PointF) animation.getAnimatedValue();
                                ball.setX(currect.x);
                                ball.setY(currect.y);
                                //ball.layout(ball.getLeft()+(int)currect.x,ball.getTop()+(int)currect.y,ball.getRight()+(int)currect.x,ball.getBottom()+(int)currect.y);
                                ball.postInvalidate();
                                root.postInvalidate();
                            }
                        });
                        animator.addListener(this);
                    }else {
                        Boolean isCal = false;
                        if (lastLeft - nowLeft < 0 && lastTop - nowTop < 0 && !isCal) {
                            isCal = true;
                            endX = root.getWidth() - nowRight;
                            float m = (float) (root.getWidth() - nowRight + ball.getWidth()/2);
                            float n = (float) (root.getWidth() - lastRight + ball.getWidth()/2);
                            float ratio = m / (n - m);
                            float endY_f = (nowTop - lastTop)* ratio;
                            endY = (int) endY_f;
                            if((endY_f + nowButton) > root.getHeight())
                            {
                                endY = root.getHeight() - nowButton;
                                float ratio2 = (endY_f - lastButton) / endY_f;
                                endX = (int)((root.getWidth() - nowRight)*(1-ratio2));
                            }
                        }
                        if (lastLeft - nowLeft < 0 && lastTop - nowTop > 0 && !isCal) {
                            isCal = true;
                            endX = root.getWidth() - nowRight;
                            float m = (float) (root.getWidth() - nowRight + ball.getWidth()/2);
                            float n = (float) (root.getWidth() - lastRight + ball.getWidth()/2);
                            float ratio = m / (n - m);
                            float endY_f = (lastTop - nowTop)* ratio;
                            endY = 0 - (int) endY_f;
                            if(endY_f > nowTop)
                            {
                                endY = 0 - (nowTop);
                                float ratio2 = (endY_f - nowButton) / endY_f;
                                endX = (int)((nowRight)*(1-ratio2));
                            }
                        }
                        if (lastLeft - nowLeft > 0 && lastTop - nowTop > 0 && !isCal) {
                            isCal = true;
                            endX =0 - nowLeft;
                            float m = (float) (nowLeft + ball.getWidth()/2);
                            float n = (float) (lastLeft + ball.getWidth()/2);
                            float ratio = m / (n - m);
                            float endY_f = (lastTop - nowTop)* ratio;
                            endY = 0 - (int) endY_f;
                            if(endY_f > nowTop)
                            {
                                endY = 0 - (nowTop);
                                float ratio2 = (endY_f - nowButton) / endY_f;
                                endX = 0 - (int)((nowLeft)*(1-ratio2));
                            }
                        }
                        if (lastLeft - nowLeft > 0 && lastTop - nowTop < 0 && !isCal) {
                            isCal = true;
                            endX = 0 - nowLeft;
                            float m = (float) (nowLeft + ball.getWidth()/2);
                            float n = (float) (lastLeft + ball.getWidth()/2);
                            float ratio = m / (n - m);
                            float endY_f = (nowButton - lastButton)* ratio;
                            endY = (int) endY_f;
                            if(endY_f >(root.getHeight() - nowButton))
                            {
                                endY = (root.getHeight() - nowButton);
                                float ratio2 = (endY_f - (root.getHeight() - nowButton)) / endY_f;
                                endX = 0 - (int)((nowLeft)*(1-ratio2));
                            }
                        }
                        PointF start = new PointF(ball.getX(), ball.getY());
                        PointF end = new PointF(ball.getX() + endX, ball.getY() + endY);
                        if(end.x < 0)
                        {
                            Log.e("__......","fixed");
                            end.x =  ball.getWidth()/2;
                            //end.y = endY + (0 - end.x);
                        }else if(end.x > (root.getWidth() - ball.getWidth()))
                        {
                            Log.e("__......","fixed");
                            end.x = root.getWidth() - ball.getWidth() -  ball.getWidth()/2;
                        }else if(end.y < 0)
                        {
                            Log.e("__......","fixed");
                            end.y =  ball.getWidth()/2;
                        }else if(end.y > (root.getHeight() - ball.getWidth()))
                        {
                            Log.e("__......","fixed");
                            end.y = root.getHeight() - ball.getWidth() -  ball.getWidth()/2;
                        }
                        animator = ValueAnimator.ofObject(new PointEvaluator(), start, end);
                        animator.setTarget(ball);
                        double diatane = MainActivity.getDistance(start,end);
                        double time = diatane / speed;
                        animator.setDuration((int)(time * 1000)).start();
                        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                PointF currect = (PointF) animation.getAnimatedValue();
                                ball.setX(currect.x);
                                ball.setY(currect.y);
                                //ball.layout(ball.getLeft()+(int)currect.x,ball.getTop()+(int)currect.y,ball.getRight()+(int)currect.x,ball.getBottom()+(int)currect.y);
                                ball.postInvalidate();
                                root.postInvalidate();
                            }
                        });

                        animator.addListener(this);
                    }

                    break;

            }
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void onAnimationStart(Animator animation) {
        Log.e("TAG", "onAnimationStart");
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        Log.e("TAG", "onAnimationEnd");
        //ball.layout((int)ball.getX(),(int)ball.getY(),(int)ball.getX()+ball.getWidth(),(int)ball.getY()+ball.getWidth());
        if(!isStop) {
            Message msg = new Message();
            msg.what = 2;
            msg.obj = this;
            myHandler.sendMessage(msg);
        }else {
            Log.d("======", ball.getX() + "," + ball.getY());
            isStop = false;
            Toast.makeText(getApplication(),"小球3秒后归位",Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(new Runnable(){
                public void run() {
                    ball.setX(root.getWidth()/2 - ball.getWidth()/2);
                    ball.setY(root.getHeight()/2 - ball.getWidth()/2);
                }
            }, 3000);
        }

    }

    @Override
    public void onAnimationCancel(Animator animation) {
        Log.e("TAG", "onAnimationCancel");
        isMoved = false;
    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    public static double getDistance(PointF start, PointF end)
    {
        double diffX = Math.abs(start.x - end.x);
        double diffY = Math.abs(start.y - end.y);
        return Math.sqrt(diffX * diffX+diffY*diffY);
    }
}
